﻿Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics.PackedVector

Namespace Global.Microsoft.Xna.Framework.Graphics

    Public Module Extension

        <Extension()> _
        Public Sub PreMultiplyAlphas(ByVal ret As Texture2D)
            Dim data As Byte4() = New Byte4(ret.Width * ret.Height - 1) {}
            ret.GetData(Of Byte4)(data)
            For i As Integer = 0 To data.Length - 1
                Dim vec As Vector4 = data(i).ToVector4()
                Dim alpha As Single = vec.W / 255.0F
                Dim a As Integer = CInt(vec.W)
#If WINDOWS_PHONE Then
                Dim r As Integer = CInt(alpha * vec.X)
                Dim g As Integer = CInt(alpha * vec.Y)
                Dim b As Integer = CInt(alpha * vec.Z)
#Else
                Dim r As Integer = CInt(Math.Truncate(alpha * vec.X))
                Dim g As Integer = CInt(Math.Truncate(alpha * vec.Y))
                Dim b As Integer = CInt(Math.Truncate(alpha * vec.Z))
#End If
                Dim packed = (a << 24) + (b << 16) + (g << 8) + r
                Dim bytes = BitConverter.GetBytes(packed)
                data(i).PackedValue = BitConverter.ToUInt32(bytes, 0)
            Next
            ret.SetData(Of Byte4)(data)
        End Sub

        <Extension()> _
        Public Function GetSize(ByVal [Me] As Texture2D) As Vector2
            If [Me] Is Nothing Then
                Return Vector2.Zero
            Else
                Return New Vector2([Me].Width, [Me].Height)
            End If
        End Function

        ''' <summary>
        ''' Take a snapshot for the current screen.
        ''' </summary>
        ''' <param name="Me"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Extension()> _
        Public Function TakeScreenshot(ByVal [Me] As GraphicsDevice, Optional ByVal rect As Rectangle = Nothing) As Texture2D
            Try
                Dim pp = [Me].PresentationParameters
                Using scrShot As New RenderTarget2D([Me], pp.BackBufferWidth, pp.BackBufferHeight, False, pp.BackBufferFormat, pp.DepthStencilFormat, pp.MultiSampleCount, RenderTargetUsage.DiscardContents)
                    [Me].SetRenderTarget(scrShot)

                    ' Set the alpha to full
                    Dim pos As Integer = 0
                    Dim data(scrShot.Width * scrShot.Height) As Color
                    scrShot.GetData(data)
                    For Each c In data
                        data(pos) = New Color(c.R, c.G, c.B, 255)
                        pos += 1
                    Next

                    Using result As New Texture2D([Me], scrShot.Width, scrShot.Height)
                        result.SetData(data)
                        TakeScreenshot = result
                    End Using

                    scrShot.Dispose()
                End Using
            Catch ex As Exception
                Throw ex
            End Try
        End Function

    End Module

End Namespace